
#################################################################################################

#  Description: Generating Tables and Figures from Fleming, Thomas G. (Forthcoming) 'Partisan 
#               Dealignment and Personal Vote-Seeking in Parliamentary Behaviour', Political Studies.
#  Author:      Thomas G. Fleming
#  Date:        25/08/2020
#  Note:        Requires file "MP_data.csv" to be saved in same folder

#################################################################################################

rm(list=ls())

library(ggplot2)
library(stargazer)
library(coefplot)
library(tidyr)

library(rstudioapi)
current_path <- getActiveDocumentContext()$path
setwd(dirname(current_path))

####################################################
# Loading data and generating additional variables #
####################################################

  # loads data

  data <- read.csv("./MP_data.csv")

  # puts share margin on same scale as partisanship variable
  
  data$share_margin <- 100*data$voteshare_margin 
  
  # creates indicator of whether challenger was from third party
  
  data$thirdparty.challenge <- ifelse((data$challenger == "Conservative" | data$challenger == "Labour"), 0, 1)
  
  # creates measure of the age at which MPs were first elected to parliament
  
  data$age.elected <- data$first_elected - data$born
  
  # creates combined measure of ten minute bill proposals
  
  data$PMBs.tenminute.combined <- data$PMBs.tenminute.negatived + data$PMBs.tenminute.permitted
  
  # limiting dataset to only subset of MPs
  
  reduced.data <- data[(is.na(data$by_election_out) == TRUE) &                                     # drops those who left at by-elections
                         (is.na(data$by_election_in) == TRUE) &                                    # drops those who arrive at by-elections
                         (is.na(data$party_switch1) == TRUE) &                                     # drops those who switch parties
                         (is.na(data$speakers) == TRUE) &                                          # drops (deputy) speakers
                         (is.na(data$minister) == TRUE) &                                          # drops ministers
                         (is.na(data$whip) == TRUE) &                                              # drops whips
                         (data$nation != "Northern Ireland") &                                     # drops NI constituencies
                         (data$retired != "Not applicable - by-election pending (death)") &        # drops those who died near term's end
                         (data$retired != "Not applicable - by-election pending (resignation)") &  # drops those who resigned but no by-election
                         ((data$party == "Labour") | (data$party == "Conservative")),]             # drops third party MPs 
  
##############################################################
# Figure 1 - Evolution of Partisanship in Britain, 1964-2015 #
##############################################################

  # Reducing to a single observation for each election, and just the v strong partisanship variable 
  
  partisanship.data <- reduced.data[!duplicated(reduced.data$GE_date), c("GE_date", "partisanship_verystrong")]

  partisanship.data$GE_date <- as.Date(partisanship.data$GE_date, format = '%m/%d/%Y')

  # Plotting partisanship over time
  
  ggplot(data = partisanship.data, aes(x = GE_date, y = partisanship_verystrong))+
    geom_line(size = 0.7)+
    geom_point()+
    theme_classic()+
    xlab(NULL)+
    ylim(low = 0, high = 50)+
    ylab("% very strong party identifiers\n")+
    theme(axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7), # adds rectangle
          axis.line = element_blank())                                          # removes axis lines because they reinforce the rectangle
  ggsave("./figure1.png", width = 4.5, height = 3, units = "in")
  
###################################################
# Table 1 - OLS Regression of MPs' Bill Proposals #
###################################################

  # Model 1 - just partisanship and one control variable (sitting days) 
  
  model.1.ols <- lm(PMBs.total ~
                      partisanship_verystrong +
                      sitting_days, 
                    data = reduced.data)

  # Model 2 - adds an interaction between partisanship and margin
  
  model.2.ols <- lm(PMBs.total ~ 
                      partisanship_verystrong*share_margin +
                      sitting_days,
                    data = reduced.data)

  # Model 3 - adds MP-level control variables
  
  model.3.ols <- lm(PMBs.total ~ 
                      partisanship_verystrong*share_margin + 
                      sitting_days + 
                      gov + 
                      retired + 
                      age.elected*share_margin +
                      thirdparty.challenge, 
                    data = reduced.data)

  # Model 4 - adds parliament-level control variables
  
  model.4.ols <- lm(PMBs.total ~ 
                      partisanship_verystrong*share_margin + 
                      sitting_days + 
                      gov + 
                      retired + 
                      age.elected*share_margin +
                      thirdparty.challenge +
                      party_polarisation*share_margin, 
                    data = reduced.data)
  
  # generating regression table of these four models as html file
  
  table.vars = c("partisanship_verystrong", "share_margin", "partisanship_verystrong:share_margin", "Constant")
  
  stargazer(model.1.ols, 
            model.2.ols, 
            model.3.ols, 
            model.4.ols, 
            type = "html",
            title = "OLS regression of MPs' bill proposals",
            out = "./table1.html",
            keep = paste0("^", table.vars, "$"),
            order = paste0("^", table.vars, "$"),
            covariate.labels = c("Partisanship", "Margin", "Partisanship*Margin", "Constant"),
            keep.stat = c("n", "rsq"),
            no.space = TRUE,
            dep.var.caption = "\\textit{Dependent Variable: Bill proposals}",
            dep.var.labels.include = FALSE,
            add.lines = list(c("Sitting days", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"),
                             c("MP controls", "", "", "\\checkmark", "\\checkmark"),
                             c("Parliament controls", "", "", "", "\\checkmark")),
            font.size = "small")
  
#####################################################################################
# Figure 2 - Conditional Relationship between Electoral Security and Bill Proposals #
#####################################################################################

  # generating data frame of values to predict from
  
  values <- data.frame(share_margin = rep(seq(from = min(reduced.data$share_margin), to = 40, length.out = 80), 2), 
                       partisanship_verystrong = rep(c((mean(reduced.data$partisanship_verystrong) + sd(reduced.data$partisanship_verystrong)), 
                                              (mean(reduced.data$partisanship_verystrong) - sd(reduced.data$partisanship_verystrong))), each = 80), 
                       sitting_days = mean(reduced.data$sitting_days),
                       gov = "Opposition party (before any switches)",
                       retired = "Sought re-election",
                       age.elected = mean(reduced.data$age.elected),
                       thirdparty.challenge = 0,
                       party_polarisation = mean(reduced.data$party_polarisation))
  
  # calculates predicted counts and 95 % CIs from these values, and combines them
  
  predictions <- cbind(values, predict(model.4.ols, values, se.fit=TRUE))
  predictions <- within(predictions, {
    count <- fit
    lower_CI <- fit - 1.96 * se.fit
    upper_CI <- fit + 1.96 * se.fit
  })
  
  # re-formatting variables for presentation purposes
  
  predictions$very_strong_id <- ifelse(predictions$partisanship_verystrong == 
                                (mean(reduced.data$partisanship_verystrong) - sd(reduced.data$partisanship_verystrong)), "Low Voter Partisanship",
                                ifelse(predictions$partisanship_verystrong == 
                                (mean(reduced.data$partisanship_verystrong) + sd(reduced.data$partisanship_verystrong)), 
                                "High Voter Partisanship", NA))
  
  # produces faceted plot of predicted counts and 95% CIs
  
  ggplot()+
    geom_line(data = predictions, aes(x = share_margin, y = count))+
    geom_line(data = predictions, aes(share_margin, y = lower_CI), linetype="dotted")+
    geom_line(data = predictions, aes(share_margin, y = upper_CI), linetype="dotted")+
    facet_grid(. ~ very_strong_id)+
    theme_classic()+
    labs(x = "\nSeat margin (% vote)", y = "Predicted bill proposals")+
    ylim(low = 0.5, high = 1.5)+
    theme(axis.title.x = element_text(face = "bold"),
          axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7), # adds rectangle
          axis.line = element_blank())                                          # removes axis lines because they reinforce the rectangle
  ggsave("./figure2.png", width = 6, height = 4, units = "in")
  
#############################################################
# Table 2 - OLS Regression of MPs' Bill Proposals (by Type) #
#############################################################

  # Model 5 - repeating Model 4 on only presentation bills
  
  model.4.presentation.ols <- lm(PMBs.presentation ~
                                   partisanship_verystrong*share_margin +
                                   sitting_days +
                                   gov +
                                   retired +
                                   age.elected*share_margin +
                                   thirdparty.challenge +
                                   party_polarisation*share_margin,
                                 data = reduced.data)
  
  # Model 6 - repeating Model 4 on only ballot bills
  
  model.4.ballot.ols <- lm(PMBs.ballot ~
                             partisanship_verystrong*share_margin +
                             sitting_days +
                             gov +
                             retired +
                             age.elected*share_margin +
                             thirdparty.challenge +
                             party_polarisation*share_margin,
                           data = reduced.data)
  
  # Model 7 - repeating Model 4 on only ten minute rule bills
  
  model.4.tenminute.ols <- lm(PMBs.tenminute.combined ~
                                partisanship_verystrong*share_margin +
                                sitting_days +
                                gov +
                                retired +
                                age.elected*share_margin +
                                thirdparty.challenge +
                                party_polarisation*share_margin,
                              data = reduced.data)
  
  # generating regression table of these three models as html file
  
  table.vars = c("partisanship_verystrong", "share_margin", "partisanship_verystrong:share_margin", "Constant")
  
  stargazer(model.4.presentation.ols, 
            model.4.ballot.ols, 
            model.4.tenminute.ols,
            type = "html",
            title = "OLS regression of MPs' bill proposals (by type)",
            out = "./table2.html",
            keep = paste0("^", table.vars, "$"),
            order = paste0("^", table.vars, "$"),
            covariate.labels = c("Partisanship", "Margin", "Partisanship*Margin", "Constant"),
            keep.stat = c("n", "rsq"),
            no.space = TRUE,
            dep.var.caption = "\\textit{Dependent Variable: Bill proposals (by type)}",
            dep.var.labels.include = TRUE,
            dep.var.labels = c("\\underline{Presentation}", "\\underline{Ballot}", "\\underline{Ten Minute}"),
            model.numbers = FALSE,
            column.labels = c("(5)", "(6)", "(7)"),
            add.lines = list(c("Sitting days", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"),
                             c("MP controls", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"),
                             c("Parliament controls", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark")),
            font.size = "small")
  
####################################################################################
# Figure 3 - Relationship between Electoral Security and Bill Proposals, 1964-1983 #
####################################################################################

  # formatting data for presentation in plot
  
  reduced.data$share_margin.1964 <- reduced.data$share_margin
  reduced.data$share_margin.1966 <- reduced.data$share_margin
  reduced.data$share_margin.1970 <- reduced.data$share_margin
  reduced.data$share_margin.1974.oct <- reduced.data$share_margin
  reduced.data$share_margin.1979 <- reduced.data$share_margin
  
  # bivariate OLS regressions for each parliament in this period
  
  model.1964 <- lm(PMBs.total ~
                     share_margin.1964,
                   data = reduced.data[reduced.data$yr==1964,])
  
  model.1966 <- lm(PMBs.total ~
                     share_margin.1966,
                   data = reduced.data[reduced.data$yr==1966,])
  
  model.1970 <- lm(PMBs.total ~
                     share_margin.1970,
                   data = reduced.data[reduced.data$yr==1970,])
  
  model.1974.oct <- lm(PMBs.total ~
                         share_margin.1974.oct,
                       data = reduced.data[(reduced.data$yr==1974 & reduced.data$mn=="October"),])
  
  model.1979 <- lm(PMBs.total ~
                     share_margin.1979,
                   data = reduced.data[reduced.data$yr==1979,])
  
  # plotting the coefficients from these models
  
  multiplot(model.1964, model.1966, model.1970, model.1974.oct, model.1979, 
            intercept = FALSE,
            innerCI = 1.645, # 90% CI
            outerCI = 0,
            lwdInner = 0.75,
            horizontal = TRUE,
            title = NULL,
            ylab = "\nParliaments",
            xlab = "Coefficients",
            zeroColor="black",
            zeroLWD=0.5,
            pointSize = 2,
            newNames = c("share_margin.1964" = "1964-66",          
                         "share_margin.1966" = "1966-70",
                         "share_margin.1970" = "1970-74",
                         "share_margin.1974.oct" = "1974-79",
                         "share_margin.1979" = "1979-1983"))+
    scale_color_manual(values = c("grey65", "grey65", "black", "black", "grey65"))+
    theme_classic()+
    theme(legend.position = "none",
          axis.title.x = element_text(face = "bold"),
          axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7), # adds rectangle
          axis.line = element_blank())                                          # removes axis lines because they reinforce the rectangle
  ggsave("./figure3.png", width = 6, height = 3, units = "in")
  
##############################################################################################
# Table 3 - Relationship between Electoral Security and Bill Proposals Before and After 1974 #
##############################################################################################

  # formatting data to be included in table
  
  reduced.data$`1964-74` <- reduced.data$PMBs.total
  reduced.data$`1974-83` <- reduced.data$PMBs.total
  reduced.data$`1964-83` <- reduced.data$PMBs.total
  
  # Model 8 - 1964-1974 analysis
  
  pre1974 <- lm(`1964-74` ~
                  voteshare_margin +
                  gov +
                  retired +
                  GE_date,
                data = reduced.data[(reduced.data$yr == 1964 |
                                     reduced.data$yr == 1966 |
                                     reduced.data$yr == 1970),])
  
  # Model 9 - 1974-1983 analysis
  
  post1974 <- lm(`1974-83` ~
                   voteshare_margin +
                   gov +
                   retired +
                   GE_date,
                 data = reduced.data[(reduced.data$yr == 1974  |
                                      reduced.data$yr == 1979),])
  
  # Model 10 - 1964-1983 analysis
  
  reduced.data$post74 <- ifelse(reduced.data$yr>=1974, 1, 0)
  
  combined <- lm(`1964-83` ~
                   voteshare_margin*post74 +
                   gov +
                   retired +
                   GE_date,
                 data = reduced.data[(reduced.data$yr == 1964 |
                                      reduced.data$yr == 1966 |
                                      reduced.data$yr == 1970 |
                                      reduced.data$yr == 1974  |
                                      reduced.data$yr == 1979),])
  
  # generating regression table of these three models as html file
  
  table.vars = c("voteshare_margin", "voteshare_margin:post74", "Constant")
  
  stargazer(pre1974, 
            post1974,
            combined,
            type = "html",
            title = "Relationship between electoral security and bill proposals before and after 1974",
            out = "./table3.html",
            keep = paste0("^", table.vars, "$"),
            order = paste0("^", table.vars, "$"),
            covariate.labels = c("Margin", "Margin*Post-1974", "Constant"),
            add.lines = list(c("Parliament FE", "\\checkmark", "\\checkmark", "\\checkmark"),
                             c("Controls", "\\checkmark", "\\checkmark", "\\checkmark")),
            no.space = TRUE,
            keep.stat = c("n", "rsq"),
            dep.var.caption = "\\textit{Dependent Variable: Bill proposals}",
            dep.var.labels.include = TRUE,
            dep.var.labels = c("\\underline{1964-74}", "\\underline{1974-83}", "\\underline{1964-83}"),
            model.numbers = FALSE,
            column.labels = c("(8)", "(9)", "(10)"),
            font.size = "small")
  
######################################################
# Figure 4 - Evolution of Party Positions, 1964-1983 #
######################################################

  # Reducing to a single observation for the relevant elections and just the position data
  
  position.data <- reduced.data[!duplicated(reduced.data$GE_date), c("GE_date", "labour_logrile", "conservative_logrile")]
  
  position.data <- position.data[1:7,]
  
  # Reformatting the data for plotting
  
  position.data <- position.data %>%
    gather(key = "party", value = "logrile", -GE_date)
  
  position.data$GE_date <- as.Date(position.data$GE_date, format = '%m/%d/%Y')
  
  position.data$party <- ifelse(position.data$party == "labour_logrile", "Labour",
                                ifelse(position.data$party == "conservative_logrile", "Conservative", NA))
  
  # Plotting these party positions over time
  
  ggplot(data = position.data, aes(x = GE_date, y = logrile, group = party, linetype = party))+
    geom_line(size = 0.7)+
    geom_point()+
    theme_classic()+
    xlab(NULL)+
    ylim(low = -2.5, high = 2.5)+
    ylab("Left-Right Position")+
    theme(legend.background = element_rect(linetype = "solid", size = 0.3, colour = "black"),
          legend.title=element_blank(),
          legend.position = c(0.15, 0.84),
          axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7), # adds rectangle
          axis.line = element_blank())                                          # removes axis lines because they reinforce the rectangle
  ggsave("./figure4.png", width = 5, height = 3, units = "in")
  
####################################################################
# Figure 5 - Age of Newly Elected Members of Parliament, 1964-1983 #
####################################################################

  # Plotting ages of newly elected politicians in this period 
  
  ggplot(reduced.data[(reduced.data$first_elected == reduced.data$yr) & (reduced.data$yr < 1987),],
         aes(x = as.factor(yr), y=age.elected))+
    geom_boxplot(size=0.3)+
    theme_classic()+
    xlab("\nGeneral elections")+
    ylab("Age of first-time MPs elected\n")+
    ylim(low = 20, high = 70)+
    theme(axis.title.x = element_text(face = "bold"),
          axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7), # adds rectangle
          axis.line = element_blank())                                          # removes axis lines because they reinforce the rectangle
  ggsave("./figure5.png", width = 6, height = 3.5, units = "in")
  
##########################################################################################
  
  rm(list=ls())
